$(function () {
    findBusinessDetail();
})

function findBusinessDetail() {
      let BDId=sessionStorage.getItem("newsDId");

      let businessCategory=myAjax(ipAddress()+"/news/category/g",{},"get",false);

      let res= myAjax(ipAddress()+"/news/g/gDetail/"+BDId,{},"get",false);

      setBusinessDetail(res.data);
      setBusinessCategory(businessCategory.data);
}

function setCurrentData(data) {
    console.log(data);
    setLocationNow(data);
    setBusinessData(data);
}

//当前位置
function setLocationNow(data) {
    let html='您的当前位置 <a href="/html/index.html">网站首页</a> ≡ <a href="/html/news.html">新闻中心</a> ';
    // let id=sessionStorage.getItem("businessCategoryId");
    let id=data.newsCategory.id;
    html+='<a style="cursor: pointer;" onclick="findbyBusinessCategory('+id+')">  ≡ '+data.newsCategory.name+'</a>';
    html+=' ≡ '+ data.name;
    //以上是设置当前位置信息.
    $("#location").html(html);
}
//设置业务信息
function setBusinessData(data){
    $("#newsTitle").html(data.name);
    $("#comeFrom").html(data.comeFrom);
    $("#writer").html(data.writer);
    let time=data.createTime.split(" ");
    $("#createTime").html(time[0]);
    $("#businessContent").html(data.content);
}




function setBusinessDetail(data) {
    // 只有一条数据时,第一条数据即为当前数据
   /* let id = sessionStorage.getItem("businessDId");
    console.log(id+"datai等等一"+data[0].id);*/
    if (data.length == 1) {
        setLastData(null);
        setCurrentData(data[0]);
        setNextData(null);
    }
    if (data.length == 2) {
        let id = sessionStorage.getItem("newsDId");
        // 第一条是当前数据则只有下一条,第一条不是当前数据,则只有上一条

        if (id == data[0].id) {
            setLastData(null);

            setCurrentData(data[0]);

            setNextData(data[1]);
        } else {
            setLastData(data[0]);

            setCurrentData(data[1]);

            setNextData(null);
        }
    }
    if (data.length == 3) {

        setLastData(data[0]);

        setCurrentData(data[1]);

        setNextData(data[2]);
    }
}
function setLastData(data) {
    let html='';
    if (data == null) {
        html = '  <div>上一个</div>\n' +
            '                        <div> 无</div>';
    }else {
        html = '  <div>上一个</div>\n' +
            '                        <div onclick="findById(' + data.id + ')">' +  data.name + '</div>';
    }
    $("#last").html(html);
}
function setNextData(data) {
    let html = '';
    if (data == null) {
        html = '  <div>下一个</div>\n' +
            '                        <div> 无</div>';
    } else {
        html = '  <div>下一个</div>\n' +
            '                        <div onclick="findById(' + data.id + ')">' + data.name + '</div>';
    }
    $("#next").html(html);

}
function findById(data){
    sessionStorage.setItem("newsDId",data);
    findBusinessDetail();
}


// 设置业务范围右边的导航栏
function setBusinessCategory(data) {
    let html='<div>新闻中心</div>';
    for(let i=0;i<data.length;i++){
        html+='<div onclick="findbyBusinessCategory('+data[i].id+')">'+data[i].name+'</div>';
    }
    html+='<div><img src="../img/zs_2.jpg"></div>';
    $("#right-navigate").html(html);
}
//


function findbyBusinessCategory(bcId){
    sessionStorage.setItem("newsCategoryid",bcId);
    window.location.href="/html/news.html";
}
